// import store from '@/store'
import axiosInstance from '../axiosInstance'

//  axios 传参params,data
//  params的对象参数名和值，axios源码会把参数和值，拼接在url？后面给后台（query查询字符串）
//  data的对象参数名和值，axios源码会把参数和值，拼接在请求体里（body参数）

// 注册
export const registerApi = ({ username, password, repassword }) => {
  return axiosInstance({
    url: '/api/reg',
    method: 'POST',
    data: { username, password, repassword }
  })
}

// 登录
export const loginApi = ({ username, password }) => {
  return axiosInstance({
    url: '/api/login',
    method: 'POST',
    data: { username, password }
  })
}

// 用户信息
export const getUserInfoApi = () => {
  return axiosInstance({
    url: '/my/userinfo',
    // 不写默认get请求
    method: 'GET'
    // 请求头
    // headers: {
    //   Authorization: store.state.token
    // }
  })
}

// 修改用户信息
export const updateUserInfoAPI = ({
  id,
  email,
  nickname,
  user_pic,
  username
}) => {
  return axiosInstance({
    url: '/my/userinfo',
    method: 'PUT',
    data: {
      id,
      email,
      nickname,
      user_pic,
      username
    }
  })
}

// 上传用户头像
export const upUserAvatarApi = (avatar) => {
  return axiosInstance({
    url: '/my/update/avatar',
    method: 'PATCH',
    data: { avatar }
  })
}

// 修改用户密码
export const updatePwdAPI = ({ old_pwd, new_pwd, re_pwd }) => {
  return axiosInstance({
    url: '/my/updatepwd',
    method: 'PATCH',
    data: { old_pwd, new_pwd, re_pwd }
  })
}

// 布局信息
export const getMenusAPI = () => {
  return axiosInstance({
    url: '/my/menus'
    // headers: {
    //   Authorization: store.state.token
    // }
  })
}

// 获取文章信息
export const getArtCateListAPI = () => {
  return axiosInstance({
    url: '/my/cate/list'
  })
}

// 添加文章分类
export const addArtCateAPI = ({ cate_name, cate_alias }) => {
  return axiosInstance({
    url: '/my/cate/add',
    method: 'POST',
    data: { cate_name, cate_alias }
  })
}

// 修改文章分类
export const updateArtCateAPI = ({ id, cate_name, cate_alias }) => {
  return axiosInstance({
    url: '/my/cate/info',
    method: 'PUT',
    data: { id, cate_name, cate_alias }
  })
}

// 删除文章分类
export const delArtCateAPI = (id) => {
  return axiosInstance({
    url: '/my/cate/del',
    method: 'DELETE',
    params: { id }
  })
}

// 发布文章
export const uploadArticleAPI = (pubForm) => {
  return axiosInstance({
    url: '/my/article/add',
    method: 'POST',
    data: pubForm
  })
}

// 获取文章列表
export const getArticleListAPI = ({ pagenum, pagesize, cate_id, state }) => {
  return axiosInstance({
    url: '/my/article/list',
    params: { pagenum, pagesize, cate_id, state }
  })
}

// 获取文章详细
export const getArticleDetailFn = (id) => {
  return axiosInstance({
    url: '/my/article/info',
    params: { id }
  })
}

// 删除文章
export const delArticleAPI = (id) => {
  return axiosInstance({
    url: '/my/article/info',
    method: 'DELETE',
    params: { id }
  })
}
